package com.ruoyi.iot.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.iot.mapper.DeviceMapper;
import com.ruoyi.iot.mapper.ProductMapper;
import com.ruoyi.iot.domain.Device;
import com.ruoyi.iot.domain.Product;
import com.ruoyi.iotcommon.constant.DeviceConstant;

import jakarta.annotation.Resource;

import com.ruoyi.iot.service.IDeviceService;
import org.springframework.util.CollectionUtils;

/**
 * 设备Service业务层处理
 * 
 * @author 常康
 * @date 2025-04-03
 */
@Service
public class DeviceServiceImpl implements IDeviceService {

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private ProductMapper productMapper;

    /**
     * 查询设备
     * 
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public Device selectDeviceById(Long id){
        return deviceMapper.selectDeviceById(id);
    }

    /**
     * 查询设备列表
     * 
     * @param device 设备
     * @return 设备
     */
    @Override
    public List<Device> selectDeviceList(Device device){
        return deviceMapper.selectDeviceList(device);
    }

    /**
     * 新增设备
     * 
     * @param device 设备
     * @return 结果
     */
    @Override
    public int insertDevice(Device device){
        // 设备标识为空自动生成标识
        if(StringUtils.isBlank(device.getKey())){
            device.setKey(IdUtils.getDeviceKey());
        }
        // 校验是否存在一样的key
        if (selectDeviceByKey(device.getProductKey(),device.getKey()) != null) {
            throw new ServiceException( "设备key已存在!");
        }
        Product product = productMapper.selectProductById(device.getProductId());
        device.setProductKey(product.getKey());

        device.setSecret(IdUtils.fastSimpleUUID());
        device.setCreateTime(DateUtils.getNowDate());
        device.setStatus(DeviceConstant.STATUS_UNACTIVE);
        return deviceMapper.insertDevice(device);
    }

    /**
     * 修改设备
     * 
     * @param device 设备
     * @return 结果
     */
    @Override
    public int updateDevice(Device device){
        Device olDevice = selectDeviceByKey(device.getProductKey(),device.getKey());
        // 校验是否存在一样的key
        if (olDevice != null && !olDevice.getId().equals(device.getId())) {
            throw new ServiceException( "设备key已存在!");
        }
        // 所有产品不能修改
        device.setProductId(null);
        device.setUpdateTime(DateUtils.getNowDate());
        return deviceMapper.updateDevice(device);
    }

    /**
     * 批量删除设备
     * 
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByIds(Long[] ids){
        for (Long id : ids) {
            deleteDeviceById(id);
        }
        return 1;
    }

    /**
     * 删除设备信息
     * 
     * @param id 设备主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDeviceById(Long id){
        // todo 删除告警规则

        // todo 删除定时任务

        // todo 删除设备上报过的数据
        return deviceMapper.deleteDeviceById(id);
    }

    @Override
    public Device selectDeviceByKey(String productKey,String deviceKey) {
        Device device = new Device();
        device.setProductKey(productKey);
        device.setKey(deviceKey);
        List<Device> deviceList = selectDeviceList(device);
        if(CollectionUtils.isEmpty(deviceList)){
            return null;
        }
        return deviceList.get(0);
    }

    @Override
    public Device selectDeviceBySecret(String deviceKey, String secret) {
        Device device = new Device();
        device.setSecret(secret);
        device.setKey(deviceKey);
        List<Device> deviceList = selectDeviceList(device);
        if(CollectionUtils.isEmpty(deviceList)){
            return null;
        }
        return deviceList.get(0);
    }

    @Override
    public int selectDeviceCount(Device device) {
        return deviceMapper.selectDeviceCount(device);
    }

    @Override
    public int updateOnlineStatus(String productKey,String deviceKey, String status) {
        Device device = new Device();
        device.setProductKey(productKey);
        device.setKey(deviceKey);
        device.setStatus(status);
        return deviceMapper.updateDeviceByKey(device);
    }
}
